package com.xxx.file;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.stream.Stream;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/22 11:00
 * @Copyright 博为峰
 */
public class FileWriterDemo {
    public static void main(String[] args) {
//        m1();
//        m2();
        //jdk1.7 try-with-resource 语法糖  Closeable AutoCloseable
        try (FileWriter fileWriter = new FileWriter("a.txt", true);) {
            fileWriter.write("abc");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void m2() {
        FileWriter fileWriter = null;
        //
//        int i = 1 / 0;
        try {
            fileWriter = new FileWriter("a.txt");
            fileWriter.write("abc");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void m1() throws IOException {
    /*
       创建一个写字符数据的文件流对象，必须要明确存储数据的目的地
       如果文件不存在，则创建
       如果文件存在，则覆盖
     */
        FileWriter fileWriter = new FileWriter("a.txt");
        fileWriter.write("abc");
//        fileWriter.flush();
        fileWriter.close();
    }
}
